﻿using ELTE.TFEG.Hough.Wpf.Classes;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ELTE.TFEG.Hough.Wpf.Controls
{
    /// <summary>
    /// Interaction logic for ZoomableImageControl.xaml
    /// </summary>
    public partial class ZoomableImageControl : UserControl
    {
        private int DesWidth { get; set; }
        private int DesHeight { get; set; }

        public Boolean IsSelected { get; private set; }

        public ZoomableControlViewModel ViewModel { get; set; }

        public Bitmap BitmapSource { get; private set; }

        public ZoomableImageControl()
        {
            InitializeComponent();
        }

        public void ToggleSelect(bool raiseEvent)
        {
            if (!IsSelected)
            {
                this.borderControl.BorderBrush = System.Windows.Media.Brushes.Goldenrod;
            }
            else
            {
                this.borderControl.BorderBrush = System.Windows.Media.Brushes.GhostWhite;
            }
            IsSelected = !IsSelected;
            if (raiseEvent)
            {
                this.ViewModel.OnSelectChanged(this);
            }
        }

        private void SetSize(int width, int height)
        {
            this.DesWidth = width;
            this.DesHeight = height;
            this.Width = width;
            this.Height = height;
            this.grdZoomable.Width = width;
            this.grdZoomable.Height = height;
        }

        private void InitViewModel()
        {
            ViewModel = new ZoomableControlViewModel();
            this.DataContext = ViewModel;
            ViewModel.SelectCommand = new DelegateCommand(x => ToggleSelect(true));
        }

        private void InitWithSize(int width, int height)
        {
            InitializeComponent();
            this.SetSize(width, height);
            this.InitViewModel();
        }

        public ZoomableImageControl(string imagePath, int width, int height)
        {
            try
            {
                InitWithSize(width, height);
                
                Uri imageUri = new Uri(imagePath, UriKind.Absolute);
                Bitmap bmp = new Bitmap(imagePath);
                bmp.Tag = imagePath;
                this.BitmapSource = bmp;
                BitmapImage img = bmp.GetBitmapImage();
                this.imgToShow.Source = img;

                
            }
            catch (Exception ex)
            {
                
            }
        }

        public ZoomableImageControl(Bitmap bmp, int width, int height)
        {
            InitWithSize(width, height);
            this.BitmapSource = bmp;
            this.imgToShow.Source = bmp.GetBitmapImage();
        }

        public void CalculateScale()
        {
            //1. get the current size of the image
            //2. get the desired size (comes as a constructor parameter)
            double zoomLevel = Convert.ToDouble(this.DesWidth) / Convert.ToDouble(this.imgToShow.RenderSize.Width);
        }

        private ScaleTransform GetScale()
        {
            TransformGroup transformGroup = (TransformGroup)this.imgToShow.RenderTransform;
            ScaleTransform transform = (ScaleTransform)transformGroup.Children[0];
            return transform;
        }

        public void SetZoom(double zoomLevel)
        {
            ScaleTransform transform = GetScale();
            transform.ScaleX += zoomLevel;
            transform.ScaleY += zoomLevel;
        }

        private void imgToShow_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            try
            {
                double zoom = e.Delta > 0 ? .2 : -.2;
                SetZoom(zoom);
            }
            catch (Exception ex)
            {

            }

        }
    }
}
